package com.youdoneed.user.focusregion;

import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.base.common.JWTUtils;
import com.youdoneed.user.consts.UserConst;
import com.youdoneed.user.model.FocusRegion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 关注区域请求控制器。
 *
 * @author Ewing
 * @date 2017.2.8
 */
@RestController
@RequestMapping("/focusRegion")
@Api(description = "关注区域")
public class FocusRegionController {

    @Autowired
    private FocusRegionService focusRegionService;

    /**
     * 用户新增关注区域。
     */
    @ApiOperation("用户新增关注区域")
    @RequestMapping(value = "/addFocusRegion", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "名称", required = true, paramType = "query"),
            @ApiImplicitParam(name = "longitude", value = "经度位置", required = true, paramType = "query"),
            @ApiImplicitParam(name = "latitude", value = "纬度位置", required = true, paramType = "query")})
    public ResultMessage addFocusRegion(@RequestHeader(UserConst.TOKEN_KEY) String token, FocusRegion focusRegion) {
        String userId = JWTUtils.getTokenUserId(token);
        focusRegion.setUserId(userId);
        focusRegionService.addFocusRegion(focusRegion);
        return new ResultMessage();
    }

    /**
     * 根据ID获取关注区域。
     */
    @ApiOperation("根据ID获取关注区域")
    @ApiImplicitParams(@ApiImplicitParam(name = "focusRegionId", value = "关注区域ID", required = true, paramType = "query"))
    @RequestMapping(value = "/getFocusRegion", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<FocusRegion> getFocusRegion(String focusRegionId) {
        return new ResultMessage<>(focusRegionService.getFocusRegion(focusRegionId));
    }

    /**
     * 用户修改关注区域。
     */
    @ApiOperation("用户修改关注区域")
    @RequestMapping(value = "/updateFocusRegion", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiImplicitParams({@ApiImplicitParam(name = "focusRegionId", value = "关注区域ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "longitude", value = "经度位置", required = true, paramType = "query"),
            @ApiImplicitParam(name = "latitude", value = "纬度位置", required = true, paramType = "query")})
    public ResultMessage updateFocusRegion(FocusRegion focusRegion) {
        focusRegionService.updateFocusRegion(focusRegion);
        return new ResultMessage();
    }

    /**
     * 删除关注区域。
     */
    @ApiOperation("删除关注区域")
    @ApiImplicitParams(@ApiImplicitParam(name = "focusRegionId", value = "关注区域ID", required = true, paramType = "query"))
    @RequestMapping(value = "/deleteFocusRegion", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage deleteFocusRegion(String focusRegionId) {
        focusRegionService.deleteFocusRegion(focusRegionId);
        return new ResultMessage();
    }

    /**
     * 获取用户的所有关注区域。
     */
    @ApiOperation("获取用户的所有关注区域")
    @RequestMapping(value = "/userFocusRegions", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<List<FocusRegion>> userFocusRegions(@RequestHeader(UserConst.TOKEN_KEY) String token) {
        String userId = JWTUtils.getTokenUserId(token);
        List<FocusRegion> focusRegions = focusRegionService.userFocusRegions(userId);
        return new ResultMessage<>(focusRegions);
    }

    /**
     * 获取用户选择的关注区域。
     */
    @ApiOperation("获取用户选择的关注区域")
    @RequestMapping(value = "/checkedFocusRegion", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<FocusRegion> checkedFocusRegion(@RequestHeader(UserConst.TOKEN_KEY) String token) {
        String userId = JWTUtils.getTokenUserId(token);
        FocusRegion focusRegions = focusRegionService.checkedFocusRegion(userId);
        return new ResultMessage<>(focusRegions);
    }

    /**
     * 设置用户的所有关注区域。
     */
    @ApiOperation("设置用户的所有关注区域")
    @RequestMapping(value = "/setUserFocusRegions", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage setUserFocusRegions(@RequestHeader(UserConst.TOKEN_KEY) String token,
                                             @RequestBody FocusRegion[] focusRegions) {
        String userId = JWTUtils.getTokenUserId(token);
        focusRegionService.setUserFocusRegions(userId, focusRegions);
        return new ResultMessage();
    }

}
